//
//  SYS_MineVC.m
//  System_App
//
//  Created by czy001 on 2017/11/27.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_MineVC.h"
#import "ApplyViewController.h"


@interface SYS_MineVC ()<UIWebViewDelegate,UIAlertViewDelegate>
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic, strong) SYS_PublicPopView *popAlertView;

@end

@implementation SYS_MineVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=DGHColor(242, 242, 242);
    [self initWithUI];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

#pragma mark  ---------initWithUI-----
-(void) initWithUI
{
    _webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, -StatusBarHeight, SCREENWIDTH, SCREENHEIGHT-TabBarHeight+StatusBarHeight)];
    _webView.delegate=self;
    _webView.dataDetectorTypes = UIDataDetectorTypeNone;
    _webView.scrollView.showsHorizontalScrollIndicator=NO;
    _webView.scrollView.showsVerticalScrollIndicator=NO;
    _webView.backgroundColor=DGHColor(242,242 , 242);
    [self.view addSubview:_webView];
    //    NSString *session=[SYS_LoginModel instance].sessionInfo.session;
    //    UICKeyChainStore *keychainStore = [UICKeyChainStore keyChainStore];
    //    NSString *pid = [keychainStore stringForKey:@"pid"];
    //    NSURL *url = [NSURL URLWithString:FORMAT(@"http://118.178.188.69:8081/index.html#/store/?session=%@&pid=%@", session,pid)];
    NSURL *url = [NSURL URLWithString:FORMAT(@"http://118.178.188.69:8081/index.html#/my/")];
    NSString *body=nil;
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    [request setHTTPMethod: @"GET"];
    [request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
    [_webView loadRequest: request];
    
}


#pragma mark ------UIWebViewDelegate---------
- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *requestString = [[[request URL] absoluteString]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"requestString : %@",requestString);
    
    
    NSArray *components = [requestString componentsSeparatedByString:@"|"];
    NSLog(@"=components=====%@",components);
    
    
    NSString *str1 = [components objectAtIndex:0];
    NSLog(@"str1:::%@",str1);
    
    
    NSArray *array2 = [str1 componentsSeparatedByString:@"/"];
    NSLog(@"array2:====%@",array2);
    
    
    NSInteger coun = array2.count;
    NSString *method = array2[coun-1];
    NSLog(@"method:===%@",method);
    
    if ([method containsString:@"logOut"])//退出登录
    {
        
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
        [[ApplyViewController shareController] clear];
        
        //删除归档文件
        NSFileManager *defaultManager = [NSFileManager defaultManager];
        if ([defaultManager isDeletableFileAtPath:LoginFilePath]) {
            [defaultManager removeItemAtPath:LoginFilePath error:nil];
        }
        
        //删除存储信息
        [SYS_LoginModel instance].sessionInfo=nil;
        [SYS_LoginModel instance].user=nil;
        [SYS_LoginModel instance].admin=nil;
        
        if ([EMClient sharedClient].isLoggedIn) {
            __weak SYS_MineVC *weakSelf = self;
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                EMError *error = [[EMClient sharedClient] logout:YES];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf hideHud];
                    if (error != nil) {
                        [weakSelf showHint:error.errorDescription];
                    }
                    else{
                        [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO userInfo:@{@"isLogin":@"no"}];
                    }
                });
            });
        }
        else
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO userInfo:@{@"isLogin":@"no"}];
        }
        
        return NO;
    }else if ([method containsString:@"wipeCache"])//清除缓存
    {
        
        UIAlertView *alter=[[UIAlertView alloc]initWithTitle:nil message:@"清除缓存?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alter.delegate=self;
        [alter show];
        
        return NO;
    }
    return YES;
}

#pragma mark -UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        [DSUtility mbSay:@"清除成功"];
    }
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
}



@end
